package lichunUtil;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

/**
 * A wrapper. 
 * Encapsulate a int key to a double value.
 * @author lichunyang
 */
public class WrapIntValue {
	public int key;
	public double value;

	public WrapIntValue(int k, double v) {
		key =k;
		value =v;
	}
	/**
	 * A comparator that makes increasing keys.
	 * @return
	 */
	static public Comparator<WrapIntValue> getCompKey_inc() {
		return WIV_CompKey_inc.get();
	}
	/**
	 * A comparator that makes decreasing keys.
	 * @return
	 */
	static public Comparator<WrapIntValue> getCompKey_dec() {
		return WIV_CompKey_inc.get();
	}
	/**
	 * A comparator that makes the high value appear first.
	 * @return 
	 */
	static public Comparator<WrapIntValue> getCompValue_dec() {
		return WIV_CompValue_dec.get();
	}
	/**
	 * A comparator that makes the low value appear first.
	 * @return 
	 */
	static public Comparator<WrapIntValue> getCompValue_inc() {
		return WIV_CompValue_inc.get();
	}
	
	static public Map<Integer, Double> tran2map (ArrayList<WrapIntValue> list) {
		Map<Integer, Double> res = new HashMap<Integer, Double>();
		for (WrapIntValue wsv : list) {
			res.put(wsv.key, wsv.value);
		}
		return res;
	}
	
	static public Map<Integer, Double> tran2map (WrapIntValue[] list) {
		Map<Integer, Double> res = new HashMap<Integer, Double>();
		for (WrapIntValue wsv : list) {
			res.put(wsv.key, wsv.value);
		}
		return res;
	}
	
	static public ArrayList<WrapIntValue> tran2list (Map<Integer, ? extends Number> map) {
		ArrayList<WrapIntValue> res = new ArrayList<WrapIntValue>();
		for (Entry<Integer, ? extends Number> e : map.entrySet()) {
			res.add(new WrapIntValue(e.getKey(), e.getValue().doubleValue()));
		}
		return res;
	}
	
	static public WrapIntValue[] tran2array (Map<Integer, ? extends Number> map) {
		WrapIntValue[] res = new WrapIntValue[map.size()];
		int pos=0;
		for (Entry<Integer, ? extends Number> e : map.entrySet()) {
			res[pos++]=new WrapIntValue(e.getKey(), e.getValue().doubleValue());
		}
		return res;
	}
	
	public String toString() {
		StringBuilder res = new StringBuilder();
		res.append(key);
		res.append("\t");
		res.append(value);
		return res.toString();
	}
	public int hashCode() {
		return toString().hashCode();
	}
	public boolean equals (Object o) {
		if (o instanceof WrapStringValue)
			return toString().equals(o.toString());
		return false;
	}
}

class WIV_CompKey_inc implements Comparator<WrapIntValue> {
	static private WIV_CompKey_inc ins = null;
	static public WIV_CompKey_inc get() {
		if (ins==null)
			ins = new WIV_CompKey_inc();
		return ins;
	}
	
	public int compare(WrapIntValue arg0, WrapIntValue arg1) {
		return arg0.key-arg1.key;
	}
	
}

class WIV_CompKey_dec implements Comparator<WrapIntValue> {
	static private WIV_CompKey_dec ins = null;
	static public WIV_CompKey_dec get() {
		if (ins==null)
			ins = new WIV_CompKey_dec();
		return ins;
	}
	
	public int compare(WrapIntValue arg0, WrapIntValue arg1) {
		return arg1.key-arg0.key;
	}
	
}

class WIV_CompValue_dec implements Comparator<WrapIntValue> {
	static private WIV_CompValue_dec ins = null;
	static public WIV_CompValue_dec get() {
		if (ins==null)
			ins = new WIV_CompValue_dec();
		return ins;
	}
	public int compare(WrapIntValue arg0, WrapIntValue arg1) {
		if (arg0==null) return 1;
		if (arg1==null) return -1;
		if (arg1.value>arg0.value) return 1;
		if (arg1.value<arg0.value) return -1;
		return 0;
	
	}
}


class WIV_CompValue_inc implements Comparator<WrapIntValue> {
	static private WIV_CompValue_inc ins = null;
	static public WIV_CompValue_inc get() {
		if (ins==null)
			ins = new WIV_CompValue_inc();
		return ins;
	}
	public int compare(WrapIntValue arg0, WrapIntValue arg1) {
		if (arg0==null) return 1;
		if (arg1==null) return -1;
		if (arg1.value>arg0.value) return -1;
		if (arg1.value<arg0.value) return 1;
		return 0;
	
	}
}